package com.bwf.date;

import java.time.*;
import java.util.Date;
import java.util.Set;

/**
 * @Version v1.0
 * @Description: TODO
 * @Author ZHANZHONGXIN
 * @Date 2022/5/13 15:26
 * @Copyright 博为峰
 */
public class LocalDateDemo {
    public static void main(String[] args) {
//        now();
//        of();
//        milli();
//        plus();
//        period();
//        duration();

        //Date-->Instant-->LocalDateTime
        Date date1 = new Date();
        Instant instant1 = date1.toInstant();//toInstant jdk1.8 Date新增的方法
        LocalDateTime localDateTime1 = LocalDateTime.ofInstant(instant1, ZoneId.systemDefault());

        //LocalDateTime-->Date
        LocalDateTime localDateTime2 = LocalDateTime.now();
        Instant instant2 = localDateTime2.atZone(ZoneId.systemDefault()).toInstant();
        Date date2 = Date.from(instant2);
    }

    private static void duration() {
        Instant start = Instant.now();
        Instant end = start.plusSeconds(20);
        Duration duration = Duration.between(start, end);
        System.out.println(duration.getSeconds());
        System.out.println(duration.getNano());
    }

    private static void period() {
        LocalDate end = LocalDate.now();//当前时间 年 月 日
        LocalDate start = LocalDate.of(2008, 8, 8);
        Period period = Period.between(start, end);
        System.out.println("period.getYears() = " + period.getYears());
        System.out.println("period.getMonths() = " + period.getMonths());
        System.out.println("period.getDays() = " + period.getDays());
    }

    private static void plus() {
        LocalDate localDate = LocalDate.now();//当前时间 年 月 日
//        localDate = localDate.plusYears(3).plusMonths(2).plusDays(3);//加
        localDate = localDate.minusDays(2);//减
        System.out.println("localDate = " + localDate);
    }

    private static void milli() {
        Instant instant = Instant.now();//时间戳
        long second = instant.getEpochSecond();//秒
        long milli = instant.toEpochMilli();//毫秒
        int nano = instant.getNano();//纳秒
        System.out.println("nano = " + nano);
    }

    private static void of() {
        LocalDate localDate = LocalDate.of(2020, 10, 20);
        LocalTime localTime = LocalTime.of(20, 15, 30);

        LocalDateTime now = LocalDateTime.now();//默认时区当前时间
        System.out.println("now = " + now);
        ZonedDateTime zonedDateTime = now.atZone(ZoneId.of("Asia/Aden"));//指定时区的时间
        System.out.println("zonedDateTime = " + zonedDateTime);
//        ZoneId.of("Asia/Aden");
    }

    private static void now() {
        LocalDate now = LocalDate.now();//日期
        System.out.println("now = " + now);
        LocalTime time = LocalTime.now();//时间
        System.out.println("time = " + time);
        LocalDateTime dateTime = LocalDateTime.now();//日期和时间
        System.out.println("dateTime = " + dateTime);
        Instant instant = Instant.now();//时间戳
        System.out.println("instant = " + instant);
        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        System.out.println("zonedDateTime = " + zonedDateTime);
        Set<String> ids = ZoneId.getAvailableZoneIds();//获取有效的时区ID
        for (String zoneId : ids) {
            System.out.println("zoneId = " + zoneId);
        }
        ZoneId zoneId = ZoneId.systemDefault();//默认时区ID
        System.out.println("zoneId = " + zoneId);
    }
}
